//
//  FHTabBarItem.swift
//  WeiBo
//
//  Created by qfh on 16/7/12.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

class FHTabBarItem: UITabBarItem {
    
    override var badgeValue: String? {
    
        didSet {
        
            let tabBarController = valueForKey("_target") as? UITabBarController
            
            for tabBarCild in (tabBarController?.tabBar.subviews)! {
            
                
                if tabBarCild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                    for tabBarButtonCild in tabBarCild.subviews {

                        if tabBarButtonCild.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                        
                            for bageCild in tabBarButtonCild.subviews {
                            
                                if bageCild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                
                                
                                    print(bageCild)
                                    
                                    
                                    var count: UInt32 = 0
                                    
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                                    
                                    for i in 0..<count {
                                    
                                        let ivar = ivars[Int(i)]
                                        
                                        let name = ivar_getName(ivar)
                                        
                                        let type = ivar_getTypeEncoding(ivar)
                                        
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        
                                        let tpyeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        
                                        print(nameStr)
                                        print(tpyeStr)
                                        
                                        if nameStr == "_image" {
                                        
                                            bageCild.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                            
                                        }
                                    
                                    }
                                    
                                    
                                    
                                    
                                }
                                
                            
                            }
                            
                        
                        }
                        
                    
                    }
                
                    
                    
                
                }
            
            }
            
            
        
        
        
        }
    
    }

}
